<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Proyek extends CI_Controller {

    function __construct() 
	{
        parent::__construct();
        $this->load->library('flexigrid');
        $this->load->library('form_validation');
        $this->load->helper('flexigrid');
        $this->load->helper(array('form', 'url'));
        $this->load->model('proyek_model');
		$this->load->model('pegawai_model');
		$this->load->model('user_model');
		$this->cek_session();
    }

    //load Grid
	function cek_session()
	{	
		$kode_role = $this->session->userdata('role_id');
		if($kode_role == '')
		{
			redirect('login');
		}
	}
	
	public function index()
	{
		$this->grid();
	}
	
	public function grid()
	{
		$kode_role = $this->session->userdata('role_id');		
		$colModel['no'] = array('No', 30, TRUE, 'center', 0);
        $colModel['NAMA_PROYEK'] = array('Nama Proyek', 250, TRUE, 'center', 1);
        $colModel['OWNER'] = array('Owner', 150, TRUE, 'center', 1);
        $colModel['TANGGAL_MULAI_PROYEK'] = array('Tanggal Mulai Proyek', 100, TRUE, 'center', 1);
		$colModel['TANGGAL_SELESAI_PROYEK'] = array('Tanggal Selesai Proyek', 120, TRUE, 'center', 1);
		$colModel['NAMA_PEGAWAI'] = array('Nama Pegawai', 100, TRUE, 'center', 1);
		$colModel['STATUS_PROYEK'] = array('Status Proyek', 70, TRUE, 'center', 1);
        if($kode_role == 2 || $kode_role == 4)  $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);
        $colModel['Detail_Proyek'] = array('Detail_Proyek', 80, TRUE, 'center', 0);		        
			
		//setting konfigurasi pada bottom tool bar flexigrid
		 $gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Daftar Proyek',
                'showTableToggleBtn' => true
            );
							
		//menambah tombol pada flexigrid top toolbar
		if($kode_role == 2 || $kode_role == 4) 
		{			
            $buttons[] = array('tambah', 'add', 'spt_js');
            $buttons[] = array('separator');
            $buttons[] = array('hapus', 'delete', 'spt_js');
			$buttons[] = array('separator');
		}
		else
		{
			$buttons = null;
		}
				            		
		// mengambil data dari file controler ajax pada method grid_user				
			$grid_js = build_grid_js('flex1', site_url("proyek/grid_foto"), $colModel, 'NAMA_PROYEK', 'asc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
        function spt_js(com,grid)
        {
                if (com=='tambah')
                {
                        location.href='" . base_url() . "index.php/proyek/form';
                }

                if (com=='hapus')
                        {
                           if($('.trSelected',grid).length>0){
                                   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' proyek?')){
                                                var items = $('.trSelected',grid);
                                                var itemlist ='';
                                                for(i=0;i<items.length;i++){
                                                        itemlist+= items[i].id.substr(3)+',';
                                                }
                                                $.ajax({
                                                   type: 'POST',
                                                   url: '" . base_url() . "index.php/proyek/delete" . "',
                                                   data: 'items='+itemlist,
                                                   success: function(data){
                                                        $('#flex1').flexReload();
														alert(data);
                                                   }
                                                });
                                        }
                                } else {
                                        return false;
                                }
                        }

        }
        </script>
        ";


            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);			
	}
	
	/*
    function index() 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $colModel['no'] = array('No', 30, TRUE, 'center', 0);
            $colModel['NAMA_PROYEK'] = array('Nama Proyek', 200, TRUE, 'center', 1);
            $colModel['OWNER'] = array('Owner', 150, TRUE, 'center', 1);
            $colModel['TANGGAL_MULAI_PROYEK'] = array('Tanggal Mulai Proyek', 100, TRUE, 'center', 1);
			$colModel['TANGGAL_SELESAI_PROYEK'] = array('Tanggal Selesai Proyek', 120, TRUE, 'center', 1);
			$colModel['NAMA_PEGAWAI'] = array('Nama Pegawai', 100, TRUE, 'center', 1);
			$colModel['STATUS_PROYEK'] = array('Status Proyek', 70, TRUE, 'center', 1);
            $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);
            $colModel['Detail_Proyek'] = array('Detail_Proyek', 80, TRUE, 'center', 0);

            $gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Daftar Proyek',
                'showTableToggleBtn' => true
            );

            $buttons[] = array('tambah', 'add', 'spt_js');
            $buttons[] = array('separator');
            $buttons[] = array('hapus', 'delete', 'spt_js');


            // mengambil data dari file controler ajax pada method grid_foto
            $grid_js = build_grid_js('flex1', site_url("proyek/grid_foto"), $colModel, 'NAMA_PROYEK', 'asc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
        function spt_js(com,grid)
        {
                if (com=='tambah')
                {
                        location.href='" . base_url() . "index.php/proyek/form';
                }

                if (com=='hapus')
                        {
                           if($('.trSelected',grid).length>0){
                                   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' proyek?')){
                                                var items = $('.trSelected',grid);
                                                var itemlist ='';
                                                for(i=0;i<items.length;i++){
                                                        itemlist+= items[i].id.substr(3)+',';
                                                }
                                                $.ajax({
                                                   type: 'POST',
                                                   url: '" . base_url() . "index.php/proyek/delete" . "',
                                                   data: 'items='+itemlist,
                                                   success: function(data){
                                                        $('#flex1').flexReload();
														alert(data);
                                                   }
                                                });
                                        }
                                } else {
                                        return false;
                                }
                        }

        }
        </script>
        ";


            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);
        }
    }
	*/

    function grid_foto() 
	{
		$kode_role = $this->session->userdata('role_id');		
        $valid_fields = array('NAMA_PROYEK', 'OWNER', 'TANGGAL_MULAI_PROYEK', 'TANGGAL_SELESAI_PROYEK', 'NAMA_PEGAWAI', 'STATUS_PROYEK');
        $this->flexigrid->validate_post('NAMA_PROYEK', 'asc', $valid_fields);
		$records = $this->proyek_model->get_data_proyek_pegawai();
		$this->output->set_header($this->config->item('json_header'));
        $no = 0;
		
        foreach ($records['records']->result() as $row) 
		{            
			$nama_proyek = str_replace("\'", "'", $row->NAMA_PROYEK);
           			
			if($kode_role == 2 || $kode_role == 4 )
			{
				$record_items[] = array(
					$row->ID_PROYEK,
					$no = $no + 1,                
					$nama_proyek,
					$row->OWNER,
					$row->TANGGAL_MULAI_PROYEK,
					$row->TANGGAL_SELESAI_PROYEK,
					$row->NAMA_PEGAWAI,
					$row->STATUS_PROYEK,
					'<a href=\'' . base_url() . 'index.php/proyek/edit/' . $row->ID_PROYEK . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/edit.png\'></a> ',
					'<a href=\'' . base_url() . 'index.php/proyek/detail_proyek/' . $row->ID_PROYEK. '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/page.png\'></a> '
				);									
			} else if($kode_role == 3)
			{
				$record_items[] = array(
					$row->ID_PROYEK,
					$no = $no + 1,                
					$nama_proyek,
					$row->OWNER,
					$row->TANGGAL_MULAI_PROYEK,
					$row->TANGGAL_SELESAI_PROYEK,
					$row->NAMA_PEGAWAI,
					$row->STATUS_PROYEK,
					// '<a href=\'' . base_url() . 'index.php/proyek/edit/' . $row->ID_PROYEK . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/edit.png\'></a> ',
					'<a href=\'' . base_url() . 'index.php/proyek/detail_proyek/' . $row->ID_PROYEK. '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/page.png\'></a> '
				);		
			}								
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

	function form() 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $data['status'] = 'new';
            $data['failed'] = false;
			$data['pegawai'] = $this->pegawai_model->get_data_aktif_pegawai();
            $data['aa'] = '';
            $data['content'] = $this->load->view('form_proyek', $data, true);
            $this->load->view('main', $data);
        }
    }

	function upload() 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            if ($this->cek_validasi() == FALSE) 
			{
                $this->form();
            } else {				
                $data = array(
                        'NAMA_PROYEK' => $this->input->post('NAMA_PROYEK'),
						'NOMOR_KONTRAK' => $this->input->post('NOMOR_KONTRAK'),
						'OWNER' => $this->input->post('OWNER'),
						'ID_PEGAWAI' => $this->input->post('ID_PEGAWAI'),
						'JENIS_KAPAL' => $this->input->post('JENIS_KAPAL'),
						'KECEPATAN' => $this->input->post('KECEPATAN'),
						'PANJANG' => $this->input->post('PANJANG'),
						'LEBAR' => $this->input->post('LEBAR'),
						'TINGGI' => $this->input->post('TINGGI'),
						'SARAT' => $this->input->post('SARAT'),
						'RUTE_PELAYARAN' => $this->input->post('RUTE_PELAYARAN'),
						'TANGGAL_MULAI_PROYEK' => $this->input->post('TANGGAL_MULAI_PROYEK'),
						'TANGGAL_SELESAI_PROYEK' => $this->input->post('TANGGAL_SELESAI_PROYEK'),
						'STATUS_PROYEK' => $this->input->post('STATUS_PROYEK')						
                );
				
				$data = str_replace("'", "\'", $data);
				$this->proyek_model->insert($data);
				redirect('proyek');
            }
        }
    }
    
    public function edit($id) 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $data['status'] = 'edit';
            $record_artikel = $this->proyek_model->selectone($id);
            foreach ($record_artikel->result() as $artikel) 
			{
                $data['ID_PROYEK'] = $artikel->ID_PROYEK;
                $data['NAMA_PROYEK'] = $artikel->NAMA_PROYEK;
                $data['NOMOR_KONTRAK'] = $artikel->NOMOR_KONTRAK;
                $data['OWNER'] = $artikel->OWNER;
				$data['ID_PEGAWAI'] = $artikel->ID_PEGAWAI;
                $data['JENIS_KAPAL'] = $artikel->JENIS_KAPAL;
                $data['KECEPATAN'] = $artikel->KECEPATAN;
                $data['PANJANG'] = $artikel->PANJANG;
                $data['LEBAR'] = $artikel->LEBAR;
                $data['TINGGI'] = $artikel->TINGGI;
                $data['SARAT'] = $artikel->SARAT;
                $data['RUTE_PELAYARAN'] = $artikel->RUTE_PELAYARAN;
				$data['TANGGAL_MULAI_PROYEK'] = $artikel->TANGGAL_MULAI_PROYEK;
                $data['TANGGAL_SELESAI_PROYEK'] = $artikel->TANGGAL_SELESAI_PROYEK;
				$data['STATUS_PROYEK'] = $artikel->STATUS_PROYEK;
            }
            //$data['status'] = 'new';
            $data['failed'] = false;
            $data['aa'] = '';
			$data = str_replace("\'", "'", $data);
            // 
			$data['pegawai'] = $this->pegawai_model->get_data_aktif_pegawai();
			// $data['proyek'] = $this->proyek_model->get_data_all_proyek();
			$data['content'] = $this->load->view('form_proyek', $data, true);
            $this->load->view('main', $data);
        }
    }

    function update($id) 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            if ($this->cek_validasi() == FALSE) 
			{
                $this->edit($id);
            } else {																		
					$data = array(
                        'NAMA_PROYEK' => $this->input->post("NAMA_PROYEK"),                     
                        'NOMOR_KONTRAK' => $this->input->post('NOMOR_KONTRAK'),                     
                        'OWNER' => $this->input->post('OWNER'),
						'ID_PEGAWAI' => $this->input->post('ID_PEGAWAI'),                     
                        'JENIS_KAPAL' => $this->input->post('JENIS_KAPAL'),                     
                        'KECEPATAN' => $this->input->post('KECEPATAN'),                     
                        'PANJANG' => $this->input->post('PANJANG'),                     
                        'LEBAR' => $this->input->post('LEBAR'),                     
                        'TINGGI' => $this->input->post('TINGGI'),                     
                        'SARAT' => $this->input->post('SARAT'),                     
                        'RUTE_PELAYARAN' => $this->input->post('RUTE_PELAYARAN'),
                        'TANGGAL_MULAI_PROYEK' => $this->input->post('TANGGAL_MULAI_PROYEK'),
						'TANGGAL_SELESAI_PROYEK' => $this->input->post('TANGGAL_SELESAI_PROYEK'),
						'STATUS_PROYEK' => $this->input->post('STATUS_PROYEK')
						);
					$data = str_replace("'", "\'", $data);
                    $this->proyek_model->update($id, $data);
                    redirect('proyek');
            }
        }
    }
	
	function detail_proyek($id) 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $data['status'] = 'detail_proyek';
            $record_artikel = $this->proyek_model->selectone($id);
            foreach ($record_artikel->result() as $artikel) 
			{
                $data['ID_PROYEK'] = $artikel->ID_PROYEK;
                $data['NAMA_PROYEK'] = $artikel->NAMA_PROYEK;
                $data['NOMOR_KONTRAK'] = $artikel->NOMOR_KONTRAK;
                $data['OWNER'] = $artikel->OWNER;
				$data['ID_PEGAWAI'] = $artikel->ID_PEGAWAI;
                $data['JENIS_KAPAL'] = $artikel->JENIS_KAPAL;
                $data['KECEPATAN'] = $artikel->KECEPATAN;
                $data['PANJANG'] = $artikel->PANJANG;
                $data['LEBAR'] = $artikel->LEBAR;
                $data['TINGGI'] = $artikel->TINGGI;
                $data['SARAT'] = $artikel->SARAT;
                $data['RUTE_PELAYARAN'] = $artikel->RUTE_PELAYARAN;
                $data['TANGGAL_MULAI_PROYEK'] = $artikel->TANGGAL_MULAI_PROYEK;
                $data['TANGGAL_SELESAI_PROYEK'] = $artikel->TANGGAL_SELESAI_PROYEK;
				$data['STATUS_PROYEK'] = $artikel->STATUS_PROYEK;
            }
            $data['failed'] = false;
            $data['aa'] = '';
			$data = str_replace("\'", "'", $data);
			$data['pegawai'] = $this->pegawai_model->get_data_aktif_pegawai();
			$data['content'] = $this->load->view('detail_proyek', $data, true);
            $this->load->view('main', $data);
        }
    }

    function delete() 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
			$spt_ids_post_array = explode(",", $this->input->post('items'));
            foreach ($spt_ids_post_array as $id_proyek)
			{
				//cek data ustad
				if (isset($id_proyek) && $id_proyek != '') {
					$sql = $this->db->query("SELECT SUM(NAMA_PROYEK) AS CEK FROM proyek WHERE ID_PROYEK = ".$id_proyek."");
					$cek = '';
					foreach ($sql->result() as $rows)
					{
						$cek = $rows->CEK;
					}
							
					$msg='';
					if($cek > 0)
					{
						$msg ='Proses hapus gagal. Proyek masih aktif.';
					} else {
						$this->proyek_model->delete($id_proyek);
						$msg ='Data yang Anda pilih berhasil dihapus.';
					}
					$this->output->set_header($this->config->item('ajax_header'));
					$this->output->set_output($msg);
				}
			}
        }
    }
	
	function alpha_space($str)
    {
        return ( ! preg_match("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ", $str)) ? FALSE : TRUE;
    } 

    function cek_validasi() 
	{
        // Setting Rules
        $this->form_validation->set_rules('NAMA_PROYEK', 'Nama proyek', 'required');
		$this->form_validation->set_rules('NOMOR_KONTRAK', 'Nomor Kontrak', 'required');
		$this->form_validation->set_rules('OWNER', 'Owner', 'required');
		$this->form_validation->set_rules('JENIS_KAPAL', 'Jenis Kapal', 'required');
		$this->form_validation->set_rules('KECEPATAN', 'Kecepatan', 'numeric');
		$this->form_validation->set_rules('PANJANG', 'Panjang', 'numeric');
		$this->form_validation->set_rules('LEBAR', 'Lebar', 'numeric');
		$this->form_validation->set_rules('TINGGI', 'Tinggi', 'numeric');
		$this->form_validation->set_rules('SARAT', 'Sarat', 'required');
		$this->form_validation->set_rules('RUTE_PELAYARAN', 'Rute Pelayaran', 'required');
		$this->form_validation->set_rules('TANGGAL_MULAI_PROYEK', 'Tanggal Mulai proyek', 'required');
		$this->form_validation->set_rules('TANGGAL_SELESAI_PROYEK', 'Tanggal Selesai proyek', 'required');
		$this->form_validation->set_rules('STATUS_PROYEK', 'Status Proyek', 'required');	
		
        //Setting Error Message
        $this->form_validation->set_message('required', 'Field %s harus diisi.');
        $this->form_validation->set_message('numeric', 'Field %s harus diisi angka.');
		$this->form_validation->set_message('greater_than', 'Field %s harus diisi angka lebih dari 0');
		//$this->form_validation->set_message('alpha_space', 'Field %s harus diisi dengan huruf dan spasi');
		
        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }

}
